<template>
	<view>
	<!-- 	<my-tabbar :currentPage="currentPage"></my-tabbar> -->
		<view v-if="status" class="head">
			<image class="head_icon" src="../../static/tabbar/s_my.png"></image>
			<view @click="login">点击登入/注册</view>
		</view>
		<view v-else class="head">
			<image class="head_icon" :src="userInfo.headimgurl"></image>
			<view>{{userInfo.nickname}}</view>
		</view>
		<view>
			<u-cell-group>
				<u-cell title="修改个人信息" isLink url="/pages/other/update-userinfo"></u-cell>
				<u-cell title="我的发言" isLink url="/pages/other/dynamic"></u-cell>
				<!-- <u-cell
				    title="点赞"
				    isLink
				    url=""
				></u-cell>
				<u-cell
				    title="评论"
				    isLink
				    url=""
				></u-cell> -->
				<u-cell title="订单" isLink url="/pages/other/order"></u-cell>
				<u-cell title="地址管理" isLink url="/pages/other/address"></u-cell>
			</u-cell-group>
		</view>
		<view>
			<button v-if="!status" @click="exit">退出登入</button>
		</view>
	</view>
</template>

<script>
	import {
		onLoad
	} from '../../uni_modules/uview-ui/libs/mixin/mixin';
	export default {
		data() {
			return {
				currentPage: "my",
				status: true,
				userInfo: {
					phone:""
				},
			}
		},
		methods: {
			login() {
				let that = this;
				uni.login({
					"provider": "weixin",
					"onlyAuthorize": true, // 微信登录仅请求授权认证
					success: function(event) {
						const {
							code
						} = event
						//客户端成功获取授权临时票据（code）,向业务服务器发起登录请求。
						uni.request({
							url: 'http://localhost:8090/user/login',
							method: "POST",
							data: {
								code: event.code
							},
							success: (res) => {
								if (res.data.data.user.phone == null||res.data.data.user.phone=="") {
									uni.navigateTo({
										url: "/pages/login/index?openid=" + res.data.data
											.user.openid
									})
								} else {
									//缓存user
									console.log("已经注册过缓存数据到this.data")
									console.log(res.data.data.user)
									uni.setStorage({
										key: 'userInfo',
										data: res.data.data.user,
										success: function() {
											that.status = false
											
										}
									});
									that.userInfo=uni.getStorageSync("userInfo");
								}
							}
						});
					},
					fail: function(err) {
						// 登录授权失败  
						// err.code是错误码
						console.log(err)
					}
				})
			},
			exit() {
				this.status = true;
				uni.clearStorageSync("userInfo");
			},
			onShow : function() {
				let userInfo=uni.getStorageSync("userInfo");
				this.userInfo=userInfo;
				if(userInfo!=null&&userInfo!=""){
					this.status=false;
				}else{
					this.status=true;
				}	
			},
		}
	}
</script>

<style>
	.head_icon {
		width: 50px;
		height: 50px;
		border-radius: 50%;
		border: 1px black solid;
		margin-right: 10px;
	}

	.head {
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 20px;
		margin: 0;
		height: 100px;
		background-color: aquamarine;
	}
</style>